import {
    Coupon
} from './coupon-model.js';
var coupon = new Coupon();
//获取应用实例
const app = getApp()
Page({
    /**
     * 页面的初始数据
     **/
    data: {
        couponList: null,
        getAll: false,
    },
    /*
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        console.log("coupon-options========", options);

        //获取分享人的手机号
        if (options && options.inviteCode) {
            wx.setStorageSync("inviteCode", options.inviteCode);
        }

        let id = options.id || "";
        this.setData({
            activityId: id
        })
        let sessionId = wx.getStorageSync("sessionId");
        //如果没登录需要跳转到登录页面
        if (sessionId == "") {
            wx.redirectTo({
                url: '/pages/getUserInfo/getUserInfo?path=/pages/coupon/coupon&id=' + id,
            })
            return
        }

        let params = {};
        if (id && id != "") {
            params = {
                activityId: id
            }
        }
        console.log("coupon-params================", params)
        wx.showLoading({
            title: '加载中...',
        })
        coupon.getCouponList(params, res => {
            wx.hideLoading()
            if (res.status) {
                let getAll = true
                res.data.forEach(val => {
                    val.startTime = coupon.formate(val.startTime, 'yyyy-MM-dd')
                    val.endTime = coupon.formate(val.endTime, 'yyyy-MM-dd')
                    if (val.pcuEndTime && val.pcuStartTime ){
                        val.pcuEndTime = coupon.formate(val.pcuEndTime, 'yyyy-MM-dd')
                        val.pcuStartTime = coupon.formate(val.pcuStartTime, 'yyyy-MM-dd')
                    }
                    if (val.isReceived == 0) {
                        getAll = false
                    }
                })

                this.setData({
                    couponList: res.data,
                    getAll
                })
            } else {
                wx.showToast({
                    title: '服务器错误',
                    icon: 'none'
                })
            }
        })
    },
    getCoupon(e) {
        let id = coupon.getDataSet(e, 'id')
        let type = coupon.getDataSet(e, 'type')
        let idx = coupon.getDataSet(e, 'idx')
        let {
            couponList
        } = this.data
        let params
        wx.showLoading({
            title: '加载中...',
        })
        let idList = []
        couponList.forEach(val => {
            idList.push(val.id)
            // if (val.isReceived == 0){
            // }
        })
        params = {
            "couponIds": idList.join(',')
        }
        // if (idList.length == 0 ){
        //     wx.showToast({
        //         icon:'none',
        //         title: '优惠券已经全部被领取过了！',
        //     })
        //     return
        // }
        console.log('params', params)
        coupon.getCoupon(params, res => {
            wx.hideLoading()
            if (res.status) {
                if (res.data.length == 0) {
                    wx.showToast({
                        icon: 'none',
                        title: '优惠券已经全部被领取过了！',
                    })
                } else {
                    wx.showToast({
                        title: '领取成功',
                    })
                }


            } else {
                wx.showToast({
                    title: '领取失败',
                })
            }
        })
    },
    shareCoupon(e) {

    },
    goDatail(e) {
        let id = coupon.getDataSet(e, "id");
        wx.navigateTo({
            url: '/pages/couponShare/couponShare?id=' + id,
        })
    },
    /**
     * 用户点击右上角分享
     */

    // onShareAppMessage: function (e) {
    //     app.aldstat.sendEvent('优惠券-分享');
    //     wx.showShareMenu({
    //         withShareTicket: true
    //     });
    //     let phone = wx.getStorageSync('phone')
    //     let path = "";

    //     console.log("分享-----------------", e);
    //     if (e.target) {
    //         let id = e.target.dataset.id;
    //         console.log('id', id)
    //         path = "/pages/couponShare/couponShare?inviteCode=" + phone + "&id=" + id;
    //     } else {
    //         path = "/pages/coupon/coupon?inviteCode=" + phone + "&id=" + this.data.activityId;
    //     }

    //     return {
    //         title: "领取你的专属红包", //storeInfo.brandName,
    //         path: path,
    //         imageUrl: 'https://goods.dingdian.xin/FtTQQYao__Dpha17L5G47PDAK8LP?imageMogr2/thumbnail/400000@',
    //         success: res => {
    //             if (res.shareTickets) {
    //                 console.log(res.shareTickets);
    //                 wx.getShareInfo({
    //                     shareTicket: res.shareTickets[0],
    //                     success: d => {
    //                         console.log(d);
    //                     }
    //                 })
    //             }
    //         }
    //     }
    // }
})